#include <LiquidCrystal.h>
#include <Bounce.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//int count = 0;
long total_count = 0;
long part_count = 0;

long prevMillis = 0;

long startMillis = 0;
long distMillis = 0;
//long pauseMillis = 0;
//long passMillis = 0;

int interval = 200;

int toggle_calib_done = 0;
int calib_meters = 0;

int curr_speed = 0;
int avg_speed = 0;
byte toggle_run = 0;
byte toggle_dist = 0;
byte toggle_reverse = 0;
byte toggle_caliber = 0;

int button = 0;

static uint8_t prev_state;

//Bounce bouncer = Bounce(5, 40);

void display_main_timeT() {
  if (toggle_run) {
   long seconds = (millis() - startMillis)/1000;
   int hours = seconds/3600;
   lcd.setCursor(0, 3);
   if (seconds/3600 < 10) lcd.print("0");
   lcd.print(seconds/3600);
   lcd.print(":");
   if ((seconds/60 - hours*60) < 10) lcd.print("0");
   lcd.print((seconds/60) - hours*60);
   lcd.print(":");
   if ((seconds - (seconds/60)*60) < 10) lcd.print("0");
   lcd.print(seconds - (seconds/60)*60);

   seconds = (millis() - distMillis)/1000;
   hours = seconds/3600;
   lcd.setCursor(11, 3);
   if (seconds/3600 < 10) lcd.print("0");
   lcd.print(seconds/3600);
   lcd.print(":");
   if ((seconds/60 - hours*60) < 10) lcd.print("0");
   lcd.print((seconds/60) - hours*60);
   lcd.print(":");
   if ((seconds - (seconds/60)*60) < 10) lcd.print("0");
   lcd.print(seconds - (seconds/60)*60);
  } else {
    lcd.clear();
    lcd.setCursor(0,2);
    lcd.print("press green to start");
  }

}


void display_main() {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("T: ");
  lcd.print(total_count);
  lcd.print("    ");
  
  lcd.setCursor(11, 0);
  lcd.print("D: ");
  lcd.print(part_count);
  lcd.print("    ");
   
  lcd.setCursor(0, 2);
  lcd.print("Speed: ");
  lcd.print(curr_speed);
  
  lcd.setCursor(11, 2);
  lcd.print("Avg: ");
  lcd.print(avg_speed);
  
  //lcd.setCursor(8, 1);
  //lcd.print("AA:");
  //lcd.print(button);
  
  display_main_timeT();
}


void display_caliber() {
 lcd.clear();
 lcd.print("*");
 if (!toggle_calib_done) {
  lcd.setCursor(2,0);
  lcd.print("Impules: ");
  lcd.print(total_count);
 } else {
   lcd.clear();
   lcd.print("*");
  //button = analogRead(5);
  //if (toggle_caliber && toggle_calib_done) {
  if (analogRead(5) == 254) {
   calib_meters++;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Set meters: ");
    lcd.print(calib_meters);

   delay(500);
   while(analogRead(5)== 254) { 
    calib_meters = calib_meters + 3;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Set meters: ");
    lcd.print(calib_meters);
    delay(100);
   }
  }
  
  
  if ((button = analogRead(5)) > 337 && button < 340) {
    if (calib_meters > 0 )calib_meters--;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("Set meters: ");
    lcd.print(calib_meters);

    delay(500);

    while((button = analogRead(5)) > 337 && button < 340) {
     if (calib_meters > 0) {
       if (calib_meters > 10) {
        calib_meters = calib_meters - 3;
       } else {
         calib_meters--;
       }
     }
     lcd.clear();
     lcd.setCursor(2,0);
     lcd.print("Set meters: ");
     lcd.print(calib_meters);
     delay(100); 
    }
  }
  
  if ((button = analogRead(5)) == 511) {
    delay(300);
    //while((button = analogRead(5)) == 511);
    toggle_calib_done = 1;
    //toggle_caliber = 1;
    //toggle_run = 1;
    int curr_speed = 0;
    avg_speed = 0;
    toggle_run = 0;
    toggle_dist = 0;
    toggle_reverse = 0;
    toggle_caliber = 0;
    lcd.clear();
    lcd.setCursor(2,0);
    lcd.print("press green to start");
  }
  

 //}
  lcd.setCursor(2,0);
  lcd.print("Set meters: ");
  lcd.print(calib_meters);
  lcd.setCursor(1,2);
  lcd.print("press green to set");
  
 } 
}

void setup() {
 lcd.begin(20, 4);
 pinMode(A5, INPUT);
 prev_state = LOW;
 lcd.print("press green to start or left to caliber");
}

void loop() {
 unsigned long currMillis = millis();
 uint8_t state = digitalRead(7);
 if (!toggle_caliber || !toggle_calib_done) { 
   button = analogRead(5);
 }
  
 if (!toggle_caliber && !toggle_run && button == 511) { 
 toggle_run = 1;
 lcd.clear();
  startMillis = distMillis = millis();
  total_count = 0;
  part_count = 0;
 }
 
 if (!toggle_caliber && button == 253) { 
   distMillis = millis();
   part_count = 0;
 }
 
 if (!toggle_caliber && !toggle_reverse && button == 339) {
  toggle_reverse = 1;
 }
 
 if (toggle_reverse && button == 511) {
   toggle_reverse = 0;
 }
 
 if (!toggle_run && !toggle_caliber && button == 201) {
  toggle_caliber = 1;
  toggle_run = 0;
 }
 
 if (toggle_caliber && !toggle_calib_done && button == 511) {
   toggle_calib_done = 1;
 }
 
 /*
 if (toggle_caliber && toggle_calib_done) {
  if (button == 253) {
   delay(50);
   while((button = analogRead(5)) != 253) {};
    delay(50); calib_meters++;
   }
  
  if (button == 339) {
    delay(50);
    while((button = analogRead(5)) != 339) {};
     delay(50); calib_meters--;
  }
 }
*/
 
 //if (toggle_pause) {
  //distMillis = millis();
 //}
 
 if (toggle_caliber && !toggle_calib_done && state != prev_state) {
     prev_state = state;
  if (state == HIGH) {
     total_count++;
  }
 } 
 
 if(toggle_run && state != prev_state) {
  prev_state = state;
  if (state == HIGH) {
    if (!toggle_reverse) {
     total_count++; part_count++;
    } else {
      if (total_count > 0) total_count--; 
      if (part_count > 0) part_count--;
    }
  }
 }


  if (currMillis - prevMillis > interval) {
  prevMillis = currMillis;
  //count = 0;
   if (toggle_run) { 
    display_main();
   } else if (toggle_caliber) {
    display_caliber();
   }
  }
 //if (toggle_run) display_main(total_count, part_count, curr_speed, avg_speed);
}

